package com.situ.mydictionary;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.toolbox.Loger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.zip.Inflater;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;
import static com.kymjs.rxvolley.RxVolley.ContentType.JSON;
import static com.situ.mydictionary.R.id.tran_information;
import static com.situ.mydictionary.R.id.tran_input;
public class TranslateActivity extends AppCompatActivity {
    //defind
    private LinearLayout trans_linearLayout;//the information of layout
    private EditText tran_edittext; //input
    private Button trans_button;//the button of search
    private Button trans_button2;//the button of music
    private TextView trans_textview;//the contain of search
    private TextView trans_textView2;// the yinbiao of word
    private TextView trans_textView3;//the mean of word
    private MediaPlayer mediaPlayer; //mediaplayer

    private Button b1, b2, b3;

    public void search() throws UnsupportedEncodingException {
        String word = tran_edittext.getText().toString().trim();
        word = URLEncoder.encode(word, "utf-8");

        if (word != null && !"".equals(word)) {

            //可以自己去有道云申请api https://fanyi.youdao.com/openapi/，申请完放下面""
            String website = "http://fanyi.youdao.com/openapi.do?keyfrom=Georgechou&key=**********&type=data&doctype=json&version=1.1&q=" + word;

            //网络请求
            RxVolley.get(website, new HttpCallback() {
                @Override
                public void onSuccess(String s) {
                    Loger.debug("请求的数据" + s);
                    try {
                        parseJson(s);
                    } catch (JSONException e1) {

                    }
                }
            });
        }
    }

    //jie xi JSON
    private void parseJson(String s) throws JSONException {
        JSONObject json = new JSONObject(s);
        String contain = null;
        String yinbiao = null;
        String mean = null;
        if (json.getInt("errorCode") == 0) {
            contain = json.getString("query");

            if (json.has("basic")) {
                JSONObject basicObject = json.getJSONObject("basic");

                if (basicObject.has("phonetic")) {
                    yinbiao = basicObject.getString("phonetic");
                }

                if (basicObject.has("explains")) {
                    JSONArray explainArr = basicObject.getJSONArray("explains");
                    mean = explainArr.getString(0);
                    for (int i = 1; i < explainArr.length(); i++) {
                        mean += "\n" + explainArr.getString(i);
                    }
                }
            }
        }
        //使基本信息布局可见
        trans_linearLayout.setVisibility(View.VISIBLE);
        //清空信息
        trans_textview.setText("");
        trans_textView2.setText("");
        trans_textView3.setText("");

        trans_textview.setText(contain);
        trans_textView2.setText(yinbiao);
        trans_textView3.setText(mean);
    }

    void player() throws IOException {
        String word = tran_edittext.getText().toString().trim();
        final String contain = "http://dict.youdao.com/dictvoice?audio=" + word;

        mediaPlayer.setDataSource(this, Uri.parse(contain));
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.reset();
            }
        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.translate_layout);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        trans_linearLayout = (LinearLayout) findViewById(tran_information);
        tran_edittext = (EditText) findViewById(tran_input);
        trans_button = (Button) findViewById(R.id.tran_search);
        trans_button2 = (Button) findViewById(R.id.tran_music);
        trans_textview = (TextView) findViewById(R.id.tran_contain);
        trans_textView2 = (TextView) findViewById(R.id.tran_yinbiao);
        trans_textView3 = (TextView) findViewById(R.id.tran_mean);
        //查询之前基本信息不可见
        trans_linearLayout.setVisibility(View.GONE);

        this.b1 = (Button) findViewById(R.id.translation);
        this.b2 = (Button) findViewById(R.id.dictionary);
        this.b3 = (Button) findViewById(R.id.index);

        mediaPlayer = new MediaPlayer();

        b2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(TranslateActivity.this, DictionaryActivity.class);
                startActivity(intent);
                finish();
            }
        });
        b3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(TranslateActivity.this, Main_Activity.class);
                startActivity(intent);
                finish();
            }
        });
        trans_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                try {
                    search();
                } catch (UnsupportedEncodingException e) {

                }
            }
        });

        trans_button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                try {
                    player();
                } catch (IOException e) {

                }
            }
        });
    }
}
//by 1--9--1--2--0--7--6--9--1--8